<?php 
include("includes/code.php");

if (isset($_GET['id'])) { $id= $_GET['id']; }

$title = CART;
///// headerni olamiz
include("includes/header.php"); 

// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];

switch ($action) {
	case 'add':
		if ($cart) {
			$cart .= ','.$_GET['id'];
		} else {
			$cart = $_GET['id'];
		}
		break;
	case 'delete':
		if ($cart) {
			$items = explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($_GET['id'] != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			$cart = $newcart;
		}
		break;
	case 'update':
	if ($cart) {
		$newcart = '';
		foreach ($_POST as $key=>$value) {
			if (stristr($key,'qty')) {
				$id = str_replace('qty','',$key);
				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
				$newcart = '';
				foreach ($items as $item) {
					if ($id != $item) {
						if ($newcart != '') {
							$newcart .= ','.$item;
						} else {
							$newcart = $item;
						}
					}
				}
				for ($i=1;$i<=$value;$i++) {
					if ($newcart != '') {
						$newcart .= ','.$id;
					} else {
						$newcart = $id;
					}
				}
			}
		}
	}
	$cart = $newcart;
	break;
}
$_SESSION['cart'] = $cart;
?>
<!-- content and sidebar -->
<div id="main">
<table class="main_table" cellspacing="0" cellpadding="0">
  <tr valign="top">
    <td class="left_col">
<!-- ------------- left start ------------- -->
<?php include("includes/left.php"); ?>
    
<!-- ----------------- left finish ---------------- -->
    </td>
    <td>
<div class="content">
<!-- content start -->

<h4 class="sarlavha"><?php echo $lang['cart']; ?></h4>

<!-- karzina jadval start -->
<div class="batafsil">
<?php
echo showCart();
?>
<br />

<p style="font-size:18px; float:right">
<img src="themes/default/img/money.png" align="absmiddle" /> <a href="buy_cart.php"><?php echo PURCHASE; ?></a></p>
<p style="font-size:18px;"><a href="index.php"><?php echo BUY_AGAIN; ?></a></p>
<hr style="border:1px solid #333333; clear:both" />

<!-- zakaz forma start -->
<?php 
if(isset($_POST['send']))
{
$ism = addslashes(htmlspecialchars($_POST['ism']));
$tel = htmlspecialchars($_POST['tel']);
$email = htmlspecialchars($_POST['email']);
$adres = addslashes(htmlspecialchars($_POST['adres']));
$text = addslashes(htmlspecialchars($_POST['matn']));
$antibot = $_POST['antibot'];

$date_now = date("d-F, Y, H:i");
$saqlash = addslashes(showCart_save());

	if(trim($ism) == '')
	{
	$error = ERR_NAME;
	}
	else if(trim($tel) == '')
	{
	$error = ERR_TEL;
	}
	else if(trim($email) == '')
	{
	$error = ERR_EMAIL;
	}
	else if(!preg_match("/([A-Z0-9_-]+\@[A-Z0-9_-]+\.[a-z.]{2,7}+)+$/i",$email))
	{
	$error = ERR_EMAIL2;
	}
	else if(trim($antibot) == '')
	{
	$error = ERR_NOBOT;
	}
	else if(trim($antibot) != BOT)
	{
	$error = ERR_BOT;
	}
	if($error == '')
    {
	/* zakazni bazaga kirit va emailga yubor */
	$natija = mysql_query("INSERT into zakaz(tovar,ism,tel,email,adres,date,text) 
    VALUES('$saqlash','$ism','$tel','$email','$adres','$date_now','$text')");
    if(!$natija) { myerror("Xatolik !!! Siz yuborgan malumotlar qabul qilinmadi,<br> iltimos, bizga qo'ng'iroq qiling");}
// emailga yuboramiz
    $to = "$row_contact->email";
	$to2 = "$row_contact->email2";
    
	$subject = SITEURL." Saytdan tovar zakaz qilindi";
    $message = "Adminga kiring. \n ism:".$ism."\n Tel:".$tel."\n E-Mail:".$email." \n Adres:".$adres."\n Fikr:".$text."\n";
    
    mail($to, $subject, $message,"From: $email \r\nReply-To: $email\r\nReturn-Path: $email\r\n");
    mail($to2, $subject, $message,"Content-type:text/plain; Charset=windows-1251 \r\n");
	/* emailga yuborish tamom */
?>
   <div style="font-size:18px; text-align:center; color:#0099CC;">
   		<? echo THANK." " .stripslashes($ism). "<br />". THANK2 ;?>
   </div>
<?php
   }
}

if(!isset($_POST['send']) || $error != '')
{
?>
<div class="zakaz">
   <p class="order_title"><?php echo ORDER; ?></p>
   <? if($error) echo "<p class='error'>$error</p>"; ?>
<form action="" method="post" name="form_buy" onSubmit="return tekshir(this)">
<table width="400" border="0" cellspacing="15" cellpadding="0">
  <tr>
    <td width="200" class="form_r"><?php echo ISM; ?></td>
    <td><input type="text" name="ism" size="30" maxlength="30"  value="<?= stripslashes($ism);?>" />
    </td>
  </tr>
  <tr>
    <td class="form_r"><?php echo TEL; ?></td>
    <td><input type="text" name="tel" size="30" maxlength="30"  value="<?=$tel;?>" />
      <br />
      <span class="minitext"><?php echo MASALAN; ?> (+99897) 123 45 67</span></td>
  </tr>
  <tr>
    <td class="form_r"><?php echo EMAIL; ?></td>
    <td><input type="text" name="email" size="30" maxlength="30" value="<?=$email;?>" /></td>
  </tr>
  <tr>
    <td class="form_r"><?php echo ADRES; ?><br /></td>
    <td><input type="text" name="adres" size="30"  value="<?= stripslashes($adres);?>" />
    <br /><span class="minitext"> <?php echo MASALAN; ?> Toshkent, Yunusobod, 19-kvartal...</span>
    </td>
  </tr>
  <tr>
    <td class="form_r"><?php echo QUSHIMCHA; ?><br /></td>
    <td><textarea name="matn" cols="30" rows="3" wrap="virtual"><?= stripslashes($text); ?></textarea>
    <br /><span class="minitext"> <?php echo MASALAN; ?> Qo'shimcha Savol yoki taklif</span>
    </td>
  </tr>
  <tr>
    <td class="form_r"><?php echo ENTER_BOT; ?> <u><?php echo BOT; ?></u></td>
    <td><input type="text" name="antibot" size="7" value="<?=$antibot;?>" /> <br />
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="send" value="<?php echo BUY; ?>" class="submit" style="cursor:pointer;" /></td>
  </tr>
</table>
</form>
</div>
<?php
}
?>
<!-- zakaz forma finish -->

</div>
<!-- karzina jadval finish // -->

<!-- content finish -->
</div>
    </td>
    <td class="right_col">
<!-- -------- right start ----------- -->
<?php include("includes/right.php"); ?>
<!-- --------- right finish ------- -->
    </td>
  </tr>
</table>

</div>
<!-- content and sidebar finish -->

</div>
<!-- main container finish -->
<?php include("includes/footer.php"); ?>